/************************************************************************
 *                                                                      *
 * Netcwmp/Opencwmp Project                                             *
 * A software client for enabling TR-069 in embedded devices (CPE).     *
 *                                                                      *
 * Copyright (C) 2013-2014  netcwmp.netcwmp group                            *
 *                                                                      *
 * This program is free software; you can redistribute it and/or        *
 * modify it under the terms of the GNU General Public License          *
 * as published by the Free Software Foundation; either version 2       *
 * of the License, or (at your option) any later version.               *
 *                                                                      *
 * This program is distributed in the hope that it will be useful,      *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
 * GNU General Public License for more details.                         *
 *                                                                      *
 * You should have received a copy of the GNU Lesser General Public     *
 * License along with this library; if not, write to the                *
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,          *
 * Boston, MA  02111-1307 USA                                           *
 *                                                                      *
 * Copyright 2013-2014  Mr.x(Mr.x) <netcwmp@gmail.com>          *
 *                                                                      *
 ***********************************************************************/

#include "cwmpd.h"

#include <sys/ipc.h>

#include <errno.h>





#define CWMP_VALUE_UNSET -1



int              cwmp_argc;
char           **cwmp_argv;


static pool_t * cwmp_global_pool;



void cwmp_daemon()
{
    //daemon(0, 1);
}




void cwmp_getopt(int argc, char **argv)
{
    
}


static int cwmp_save_argv( int argc, char *const *argv)
{
    cwmp_argv = (char **) argv;
    cwmp_argc = argc;

    return 0;
}


int cwmp_set_var(cwmp_t * cwmp)
{
    FUNCTION_TRACE();


    cwmp_bzero(cwmp, sizeof(cwmp_t));
    cwmp->new_request = CWMP_TRUE;
    pool_t * pool = pool_create(POOL_DEFAULT_SIZE);
	if(NULL == pool)
	{
	    cwmp_log_error("pool create return null, size:POOL_DEFAULT_SIZE");
		return CWMP_ERROR;
	}
	
    cwmp->pool = pool;


#if 0
    cwmp_event_init(cwmp);

    cwmp->queue = queue_create(pool);
#endif

    return CWMP_OK;
}




#ifdef USE_CWMP_OPENSSL
void cwmp_init_ssl(cwmp_t * cwmp)
{
    char * cafile = cwmp_conf_pool_get(cwmp_global_pool, "cwmp:ca_file");
    char * capasswd = cwmp_conf_pool_get(cwmp_global_pool, "cwmp:ca_password");   
    cwmp->ssl_ctx = openssl_initialize_ctx(cafile, capasswd);
}
#endif


static int copy_file(const char* src, const char* des)
{
	int rc = CWMP_OK;
	FILE *psrc = NULL;
	FILE *pdes = NULL;
	psrc = fopen(src, "r");
	pdes = fopen(des, "w+");
 
 
	if (psrc && pdes)
	{
		int nLen = 0;
		char szBuf[1024] = {0};
		while((nLen = fread(szBuf, 1, sizeof szBuf, psrc)) > 0)
		{
			fwrite(szBuf, 1, nLen, pdes);
		}
		rc = CWMP_OK;
	}
	else
	{
		rc = CWMP_ERROR;
	}
 
 
	if (psrc)
	{
		fclose(psrc);
		psrc = NULL;
	}
 
 
	if (pdes)
	{
		fclose(pdes);
		pdes = NULL;
	}
 
 
	return rc;
}


cwmp_t *g_cwmp = NULL;


#define RO_CWMP_CONF_PATH     "/etc_ro/cwmp.conf"
#define RW_CWMP_CONF_PATH     "/cache/cwmp.conf"

int creat_configfile()
{
	FILE *fp;
	char * acs_url[64] = {0};
	char * acs_username[32] = {0};
	char * acs_password[32] = {0};
	char * periodic_enable[16] = {0};
	char * periodic_interval[16] = {0};
	char * cpe_username[32] = {0};
	char * cpe_password[32] = {0};
	char wifi_ssid[33] = {0};
	
	cfg_get_item("acs_url", acs_url, sizeof(acs_url));
	cfg_get_item("acs_username", acs_username, sizeof(acs_username));
	cfg_get_item("acs_password", acs_password, sizeof(acs_password));
	cfg_get_item("periodic_enable", periodic_enable, sizeof(periodic_enable));
	cfg_get_item("periodic_interval", periodic_interval, sizeof(periodic_interval));
	cfg_get_item("cpe_username", cpe_username, sizeof(cpe_username));
	cfg_get_item("cpe_password", cpe_password, sizeof(cpe_password));
	cfg_get_item ("SSID1", wifi_ssid, sizeof (wifi_ssid));
	
	if(strlen(acs_url) == 0)
		return -1;
	system("rm -rf /cache/cwmp.conf");
	fp = fopen(RW_CWMP_CONF_PATH, "w+");
	if(!fp)
	{
		fprintf(stderr,"fopen %s error\n", RW_CWMP_CONF_PATH);
		return -1;
	}

	fprintf(fp, "[cwmp]\n");
	fprintf(fp, "enable=1;\n");
	fprintf(fp, "soap_env=SOAP-ENV\n");
	fprintf(fp, "soap_enc=SOAP-ENC\n");
	if(((strlen(acs_username) > 0) && (strlen(acs_password) > 0)) || (access("/cache/ca.pem", F_OK) == 0))
		fprintf(fp, "acs_auth=1\n");
	else
		fprintf(fp, "acs_auth=0\n");

	if((strlen(cpe_username) > 0) && (strlen(cpe_password) > 0))
		fprintf(fp, "cpe_auth=1\n");
	else
		fprintf(fp, "cpe_auth=0\n");
	
	fprintf(fp, "acs_url=%s\n",acs_url);
	fprintf(fp, "ca_file=/cache/ca.pem\n");
	fprintf(fp, "ca_password=123456\n");
	fprintf(fp, "cpe_manufacture=DLINK\n");
	fprintf(fp, "cpe_oui=FEDCBA\n");
	fprintf(fp, "cpe_sn=744AA4E7b050\n");
	fprintf(fp, "cpe_name=MIFI\n");
	if(strstr(wifi_ssid,"930M") != NULL)
		fprintf(fp, "cpe_pc=DWR930M\n");
	else
		fprintf(fp, "cpe_pc=DWR932M\n");
	fprintf(fp, "cpe_specver=1.0\n");
	fprintf(fp, "cpe_hwver=V1.0\n");
	fprintf(fp, "cpe_version=V3.3.3.3\n");
	fprintf(fp, "cpe_username=%s\n",cpe_username);
	fprintf(fp, "cpe_password=%s\n",cpe_password);
	fprintf(fp, "acs_username=%s\n",acs_username);
	fprintf(fp, "acs_password=%s\n",acs_password);
	fprintf(fp, "\n");
	fprintf(fp, "\n");
	fprintf(fp, "[cwmpd]\n");
	fprintf(fp, "httpd_port=8090\n");
	fprintf(fp, "http_timeout=-1\n");
	fprintf(fp, "\n");
	fprintf(fp, "\n");
	fprintf(fp, "[file]\n");
	fprintf(fp, "download_root=/cache\n");
	fprintf(fp, "download_img_path=/cache/download.img\n");
	fprintf(fp, "download_cfg_path=/cache/download.cfg\n");
	fprintf(fp, "download_unknown_path=/cache/download.tmp\n");
	fprintf(fp, "upload_cfg_path=/cache/cwmp.cfg\n");
	fprintf(fp, "upload_log_path=/cache/cwmp.log\n");
	fprintf(fp, "\n");
	fprintf(fp, "\n");
	fprintf(fp, "[periodic]\n");
	if(atoi(periodic_enable))
		fprintf(fp, "enable=1\n");
	else
		fprintf(fp, "enable=0\n");
	fprintf(fp, "interval=%d\n",atoi(periodic_interval));
	fprintf(fp, "\n");
	fprintf(fp, "\n");
	fprintf(fp, "[test]\n");
	fprintf(fp, "parameterkey=123456\n");
	fprintf(fp, "stun_enable=0\n");
	fprintf(fp, "stun_serveraddress=192.168.100.3\n");
	fprintf(fp, "stun_serverport=8090\n");
	fprintf(fp, "stun_username=stun\n");
	fprintf(fp, "stun_password=stun\n");
	fprintf(fp, "stun_maximumkeepaliveperiod=100\n");
	fprintf(fp, "stun_minimumkeepaliveperiod\n");
	fprintf(fp, "natdetected=0\n");
	
	
	fclose(fp);
	return 0;
}
int main(int argc, char **argv)
{
    cwmp_pid_t pid;
    cwmp_t * cwmp;

    int syslog_enable = 0;
    int cwmp_enable = 0;

#ifdef WIN32
    WSADATA wsaData;
    WSAStartup(MAKEWORD(2, 2), &wsaData);
#endif
    pid = getpid();

//    cwmp_log_init("/var/log/cwmpd.log", CWMP_LOG_DEBUG);
    cwmp_log_init("/cache/cwmpd.log", CWMP_LOG_DEBUG_SQL);
    cwmp_global_pool = pool_create(POOL_DEFAULT_SIZE);
	if(NULL == cwmp_global_pool)
	{
	    cwmp_log_error("cwmp_global_pool pool create fail");
		return -1; 
	}

	
    cwmp = pool_palloc(cwmp_global_pool, sizeof(cwmp_t));
	if(NULL == cwmp)
	{
	    cwmp_log_error("cwmp pool palloc fail");
		return -1; 
	}


	//if(access(RW_CWMP_CONF_PATH, F_OK) != 0)
	{
		if(creat_configfile())
			copy_file(RO_CWMP_CONF_PATH, RW_CWMP_CONF_PATH);
		cwmp_log_info("%s not exist, copy %s to it", RW_CWMP_CONF_PATH, RO_CWMP_CONF_PATH);
	}


    cwmp_conf_open(RW_CWMP_CONF_PATH);
    
    cwmp_enable=cwmp_conf_get_int("cwmp:enable");
    if(!cwmp_enable)
    {
        cwmp_log_error("cwmp_enable:%d", cwmp_enable);
		return -1;    
    }

    cwmp_getopt(argc, argv);
    
    //cwmp_init_db(); 

    cwmp_set_var(cwmp);
    cwmp_daemon();
    
    cwmp_conf_init(cwmp);


    cwmp_event_init(cwmp);
    cwmp->queue = queue_create(cwmp->pool);

    g_cwmp = cwmp;

	ini_periodic_info(cwmp);
	

#ifdef USE_CWMP_OPENSSL
    cwmp_init_ssl(cwmp);
#endif

    cwmp_model_load(cwmp, "/etc_ro/device.xml");
    cwmp_process_start_master(cwmp);

    return 0;
}



